package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.ReviewConverter;
import com.cskaoyan.wordmemorize.dao.entity.UserRevStatisticsDO;
import com.cskaoyan.wordmemorize.dao.entity.UserVocRevDO;
import com.cskaoyan.wordmemorize.dao.mapper.UserRevStatisticsMapper;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocRevMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.DownLoadUserReviewDTO;
import com.cskaoyan.wordmemorize.dto.app.UserReviewStatisticsDTO;
import com.cskaoyan.wordmemorize.dto.app.UserVocRevDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserReviewDataCommand;
import com.cskaoyan.wordmemorize.request.UserReviewStatisticsCommand;
import com.cskaoyan.wordmemorize.request.UserVocRevCommand;
import com.cskaoyan.wordmemorize.service.UserReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class UserReviewServiceImpl implements UserReviewService {
    @Autowired
    UserRevStatisticsMapper userRevStatisticsMapper;
    @Autowired
    UserVocRevMapper userVocRevMapper;
    @Autowired
    ReviewConverter reviewConverter;

    @Override
    public void synchronizeUserReviewData(UserReviewDataCommand command, Long userId) {
        UserReviewStatisticsCommand statisticsCommand = command.getRevStatisticsCommand();
        List<UserVocRevCommand> vocRevCommands = command.getVocRevCommands();
        //利用转换器转换成DO对象
        UserRevStatisticsDO statisticsDO = reviewConverter.userRevStatisticsCommand2DO(statisticsCommand);
        statisticsDO.setUserId(userId);
        List<UserVocRevDO> userVocRevDOS = reviewConverter.userVocRevCommands2DOs(vocRevCommands);

        System.out.println("统计的用户id"+statisticsDO.getUserId());
        LambdaQueryWrapper<UserRevStatisticsDO> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(UserRevStatisticsDO::getUserId, userId);

        UserRevStatisticsDO userRevStatisticsDO = userRevStatisticsMapper.selectOne(queryWrapper1);
        if (userRevStatisticsDO == null) {
            userRevStatisticsMapper.insert(statisticsDO);
        } else {
            userRevStatisticsMapper.update(statisticsDO, queryWrapper1);
        }
        for (UserVocRevDO userVocRevDO : userVocRevDOS) {
            userVocRevDO.setUserId(userId);
            userVocRevDO.setId(null);
            System.out.println("1111111111111111111111111111111111111111111111111111111111111111111111111111111111");
            System.out.println("单词的用户id"+statisticsDO.getUserId());
            LambdaQueryWrapper<UserVocRevDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserVocRevDO::getUserId, userId);
            queryWrapper.eq(UserVocRevDO::getVocId, userVocRevDO.getVocId());
            UserVocRevDO userVocRevDOS1 = userVocRevMapper.selectOne(queryWrapper);
            if (userVocRevDOS1== null) {
                userVocRevMapper.insert(userVocRevDO);
            } else {
                userVocRevMapper.update(userVocRevDO, queryWrapper);
            }
        }

    }

    @Override
    public DownLoadUserReviewDTO downloadUserReviewData(PageRequest pageRequest, Long userId) {
        Page<UserVocRevDO> page = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());
        Page<UserRevStatisticsDO> page1 = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());
        List<UserVocRevDO> userVocRevDOS = userVocRevMapper.selectList(page, null);
        List<UserRevStatisticsDO> userRevStatisticsDOS = userRevStatisticsMapper.selectList(page1, null);
        PageDTO<UserVocRevDTO> userVocRevDTOPageDTO = reviewConverter.userVocRevPage2PageDTO(userVocRevDOS, page.getTotal());
        PageDTO<UserReviewStatisticsDTO> userReviewStatisticsDTOPageDTO = reviewConverter.userRevStatistcsPage2PageDTO(userRevStatisticsDOS, page.getTotal());
        DownLoadUserReviewDTO downLoadUserReviewDTO = new DownLoadUserReviewDTO();
        downLoadUserReviewDTO.setUserVocRevPage(userVocRevDTOPageDTO);
        downLoadUserReviewDTO.setUserReviewStatisticsPage(userReviewStatisticsDTOPageDTO);
        return downLoadUserReviewDTO;


    }
    /*
    - 当用户选择复习一个单词，前两次为学习状态，复习间隔固定位1天，即前两天每天复习一次。前两次复习factor(我们称之为`易度因子`，始终为默认值1.4)
- 从第三次开始按照公式计算复习间隔时间(以天为单位)，如果为小数则四舍五入
- 如果连续失败三次复习的反馈都是忘记，那么对于该单词的复习重新进入学习状态(进入下一轮复习)，公式中的current_count是指本轮中单词复习次数
- factor易度因子的取值范围为[1.4, 2.5]
     */

    @Override
    public void addUserRovRev(List<Long> vocIds, Long userId) {

        for (Long vocId : vocIds) {
            LambdaQueryWrapper<UserVocRevDO> queryWrapper = new LambdaQueryWrapper<>();
            //判断是否重复添加
            queryWrapper.eq(UserVocRevDO::getUserId, userId);
            queryWrapper.eq(UserVocRevDO::getVocId, vocId);
            UserVocRevDO userVocRevDO1 = userVocRevMapper.selectOne(queryWrapper);
            if (userVocRevDO1 != null) {
                continue;
            }
            UserVocRevDO userVocRevDO = new UserVocRevDO();
            userVocRevDO.setUserId(userId);
            userVocRevDO.setVocId(vocId);
            userVocRevDO.setFactor(1.4);
            userVocRevDO.setReviewInterval(1);
            userVocRevDO.setNextReviewDate(LocalDate.now().plusDays(1));
            userVocRevDO.setType(0);
            userVocRevDO.setFamiliarity(0);
            userVocRevDO.setConsecutiveFailCount(0);
            userVocRevDO.setTotalReview(0);
            userVocRevDO.setIsDeleted(0);
            userVocRevMapper.insert(userVocRevDO);
        }


    }
}
